import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('../layout/index.vue'),
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '仪表盘', icon: 'Monitor' }
      },
      {
        path: '/navbar',
        name: 'Navbar',
        component: () => import('../views/Navbar/index.vue'),
        meta: { title: '顶部栏管理', icon: 'Menu' }
      },
      {
        path: '/home-settings',
        name: 'HomeSettings',
        component: () => import('../views/HomeSettings/index.vue'),
        meta: { title: '首页配置', icon: 'Setting' }
      },
      {
        path: '/about-settings',
        name: 'AboutSettings',
        component: () => import('../views/AboutSettings/index.vue'),
        meta: { title: '关于我们配置', icon: 'InfoFilled' }
      },
      {
        path: '/about-main-settings',
        name: 'AboutMainSettings',
        component: () => import('../views/about/AboutMainSettings/index.vue'),
        meta: { title: '关于我们主页', icon: 'InfoFilled' }
      },
      {
        path: '/about-advantage-settings',
        name: 'AboutAdvantageSettings', 
        component: () => import('../views/about/AboutAdvantageSettings/index.vue'),
        meta: { title: '企业优势', icon: 'TrendCharts' }
      },
      {
        path: '/about-quality-settings',
        name: 'AboutQualitySettings',
        component: () => import('../views/about/AboutQualitySettings/index.vue'),
        meta: { title: '质量保证', icon: 'Medal' }
      },
      {
        path: '/products-settings',
        name: 'ProductsSettings',
        component: () => import('../views/ProductsSettings/index.vue'),
        meta: { title: '产品配置', icon: 'Goods' }
      },
      {
        path: '/products-main-settings',
        name: 'ProductsMainSettings',
        component: () => import('../views/products/ProductsMainSettings/index.vue'),
        meta: { title: '产品页面配置', icon: 'Goods' }
      },
      {
        path: '/products-list-settings',
        name: 'ProductsListSettings',
        component: () => import('../views/products/ProductsListSettings/index.vue'),
        meta: { title: '产品列表管理', icon: 'List' }
      },
      {
        path: '/contact',
        name: 'Contact',
        component: () => import('../views/Contact/index.vue'),
        meta: { title: '联系我们', icon: 'Message' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin_token')
  
  if (to.meta.requiresAuth !== false && !token) {
    next('/login')
  } else if (to.path === '/login' && token) {
    next('/')
  } else {
    next()
  }
})

export default router